package metagrammar;

import java.util.UUID;
import java.util.concurrent.ExecutionException;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import jaxb.metagrammar.OperatorToUUIDStringAdapter;
import util.HasId;
import util.Named;
import application.Application;
import application.GlobalContext;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class OperatorDescription implements HasId, Named {
    
    public static OperatorDescription undefinedOperator = new OperatorDescription(
            Operator.undefinedOperator, "Undefined");
    
    public static OperatorDescription getDescription(Operator operator)
    {
        if(operator == Operator.undefinedOperator) {
            return undefinedOperator;
        }
        try {
            return Application.injector.getInstance(GlobalContext.class).getDescription(operator);
        }
        catch(ExecutionException e) {
            return null;
        }
    }
    
    @XmlJavaTypeAdapter(OperatorToUUIDStringAdapter.class)
    private final Operator operator;
    
    private final String name;
    
    private final String description;
    
    private OperatorDescription()
    {
        operator = null;
        name = description = null;
        // for serialization
    }
    
    public OperatorDescription(Operator operator, String name)
    {
        this.operator = operator;
        this.name = name;
        this.description = "";
    }
    
    public OperatorDescription(Operator operator, String name, String description)
    {
        this.operator = operator;
        this.name = name;
        this.description = description;
    }
    
    @Override
    public String getName()
    {
        return name;
    }
    
    public String getDescription()
    {
        return description;
    }
    
    public Operator getOperator()
    {
        return operator;
    }
    
    @Override
    public UUID getId()
    {
        return operator.getId();
    }
}
